// pages/redPacket/redPacket.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
let wxbarcode = require('../../utils/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, 
    size: 10,
    tabList: ['下单可使用红包','已使用或过期红包'],
    tabIndex: 0,
    recordList: [], //优惠卷列表
    totalPages: 0,
    barcodeModeal: false,
    code: '',
    shopId: "",//
    orderPrice: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this  = this;
    console.log(options)
    if (options.shopId != undefined){
      _this.setData({
        shopId: options.shopId,
        orderPrice: options.orderPrice,
      })
    }
    _this.getRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.loginFlag){
      this.getRecord()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //console.log("下拉加刷新")
    let _this = this;
    _this.setData({
      page: 1,
      recordList: [],
    })
    wx.showLoading({
      title: '刷新中',
    })
    _this.getRecord(function () {
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //console.log("上拉加载")
    let _this = this;
    if (_this.data.page < _this.data.totalPages) {
      _this.setData({
        page: _this.data.page + 1
      })
      wx.showLoading({
        title: '加载中',
      })
      _this.getRecord(function () {
        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },
  //tab切换
  tabChange(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    _this.setData({
      tabIndex: index,
      page: 1,
      recordList: [],
    })
    //获取红包列表
    _this.getRecord();
  },
  //获取红包列表
  getRecord(callback){
    let _this = this;
    if (_this.data.shopId != '' && _this.data.tabIndex != 1){
      _this.redOrderData(callback);
    }else{
      _this.redListData(callback);
    }
  },

  //红包列表
  redListData(callback){
    let _this = this;
    let data = {
      "role": "",
      "couponName": "",
      "drawId": "",
      "sort": "",
      "status": _this.data.tabIndex == 1 ? "Used" : "UnUsed", //未使用 UnUsed, //已使用 Used
      "customerId": "",
      "shopId": "",
      "isDel": ""
    }
    // console.log(data)
    Api.getRecord(data, _this.data.page, _this.data.size).then(res => {
      console.log(res)
      _this.setData({
        recordList: _this.data.recordList.concat(res.data.dataList)
      })
      _this.data.recordList.forEach(function (item, index, arr) {
        if (item.activiteStartTime) {
          item.activiteStartTime = util.formatTime(new Date(item.activiteStartTime))
        }
        if (item.activiteEndTime) {
          item.activiteEndTime = util.formatTime(new Date(item.activiteEndTime))
        }
        item.expireTime = util.formatTime(new Date(item.expireTime))
        _this.setData({
          recordList: arr
        })
      }); (callback && typeof (callback) === "function") && callback();
    })
  },
  //订单红包列表
  redOrderData(callback){
    let _this = this;
    console.log(_this.data.shopId);
    console.log(_this.data.orderPrice)
    Api.redOrderData(_this.data.shopId, _this.data.orderPrice).then( res => {
      console.log(res)
      _this.setData({
        recordList: _this.data.recordList.concat(res.data)
      })
      _this.data.recordList.forEach(function (item, index, arr) {
        if (item.activiteStartTime) {
          item.activiteStartTime = util.formatTime(new Date(item.activiteStartTime))
        }
        if (item.activiteEndTime) {
          item.activiteEndTime = util.formatTime(new Date(item.activiteEndTime))
        }
        item.expireTime = util.formatTime(new Date(item.expireTime))
        _this.setData({
          recordList: arr
        })
      });
      (callback && typeof (callback) === "function") && callback();
    })
  },


  //点击弹出红包的条形码开始
  showBarcodeModeal(e) {
    let _this = this;
    let index = e.currentTarget.dataset.index;
    console.log(_this.data.order )
    if (_this.data.shopId){
      console.log(index)
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面
      var prevPage = pages[pages.length - 2]; //上一个页面

      prevPage.setData({
        couponId: _this.data.recordList[index].id,
        couponText: _this.data.recordList[index].couponName
      });
      wx.navigateBack({
        delta: 1
      });
      return;
    }
    _this.setData({
      barcodeModeal: true,
      code: _this.data.recordList[index].cdkey
    });
    wxbarcode.barcode('barcode', _this.data.code, 680, 200);//YCM91128 生成条形码
  },
  //点击弹出红包的条形码结束
  //关闭点击弹出红包的条形码模态框开始
  closebarcodeModeal(e) {
    this.setData({
      barcodeModeal: false
    });
  },

})